Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

164
Visualizações
How to embed an if statement in a "for of" loop in JS

I have a genuine quetion. Given this simple JS code:

for (let x of array) {
   if (x != array[0]) {
       // do stuff
   }
}

Is there a way to embed the "if" statement directly into the for loop?

I tried something like this:

for (let x of array if x != array[0] {
   // do stuff    
}

And:

for (let x != array[0] of array) {
   // do stuff
}

But both of them didn't work. Let me know if you can find a solution!

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can filter the array with the condition and iterate through that result:

const array = [1, 2, 3]

for(let x of array.filter(e => e != array[0])){
    console.log(x)
}

about 4 years ago · Juan Pablo Isaza Relatório

0

You cant pass a boolean in a "for of" or "for in" loops , the only way of do a comparison for an iteration of a loop is doing :

let array = ["37","728"];

for (let x = 0 ; ((x < array.length) && array[x] == "37") ; x++){
  console.log(array[x]);
}

And it will break the for loop if the second condition pass to true

about 4 years ago · Juan Pablo Isaza Relatório

0

If you specifically only want to do stuff on the 2nd - nth element, you can just use a for loop and start at 1 instead of 0.

for (let i = 1; i < arr.length; i++) {
  // do stuff
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda